<template>
  <!--基本信息  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="2"
      :formType="type"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        v-if="type !== 'show'"
        @click="saveDialog"
        :loading="saveLoading"
        type="primary"
      >{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { getTimeStr } from '@/utils/utils';
import { updateFollowInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addFollowInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addApply } from '@/api/cms-api/customerManage/continueManage/record/index';
export default {
  mixins: [authority, dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  mounted() {
    var myDate = new Date(); //Thu Dec 28 2017 10:</span>07:39 GMT+0800 (中国标准时间)
    let str = getTimeStr(myDate);
    this.initForm.find(item => item.fieldName === 'applyForDate').initialValue = str;
  },
  data() {
    this.conf = { add: '提 交', edit: '修 改' };
    this.saveData = null;
    return {
      followId: this.rowData['followId'],
      projectId: this.rowData['projectId'],
      initForm: this.initFormFun(),
      saveLoading: false,
      formValue: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    initFormFun() {
      return [
        {
          type: 'DATE',
          label: '申请时间:',
          fieldName: 'applyForDate',
          valueFormat: 'yyyy-MM-dd HH:mm:ss',
          rules: [{ required: true, message: '请填写申请日期', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '延续原因:',
          fieldName: 'applyReason',
          disabled: false,
          rules: [{ required: true, message: '请填写延续原因', trigger: 'blur' }, { max: 200, message: '延续原因不能超过200', trigger: 'blur' }]
        }
      ];
    },
    async formChangeHandle(val) {
      this.formValue = val;
      if (this.formValue !== null) {
        this.saveLoading = true;
        let res = {};
        var date = "";
        if(this.formValue.applyForDate){
          date = getTimeStr(new Date(this.formValue.applyForDate));
        }
        this.formValue.applyForDate  = date;
        this.formValue = Object.assign({}, { projectId: this.projectId }, this.formValue);
        this.formValue = Object.assign({}, { applyType: '25221002' }, this.formValue);
        this.formValue = Object.assign({}, { applyStatus: '25241001' }, this.formValue);

        if (this.type === 'add') {
          res = await addApply(this.formValue);
        }
        if (this.type === 'edit') {
          res = await updateFollowInfo(this.formValue, this.followId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }else {
           this.closeDialog('refresh');
           this.$notify.warning({ title: '提示信息', message: res.errMsg });

        }
      }
    },
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }

      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      if (month < 10) {
        month = "0"+month;
      }
      var day = date.getDate();
      if (day < 10) {
        day = "0"+day;
      }
      return year + '-' + month + '-' + day + ' 00:00:00';
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
